package se.webbzon.boltzmann.game.event;

import se.webbzon.boltzmann.game.object.item.GameItemContainer;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.Sequences;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.item.ItemGroup;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;

public class PlaceItemEvent extends AbstractGameEvent {
	
	// The binding of this game event
	private final WorldObject binding;
	
	// The sequence associated with this game event
	private final Sequence sequence;

	/*============================================================
	Constructors
	============================================================*/

	/** Places the item with the item id argument[0] in the item
	 * container defined by the given binding.  **/
	public PlaceItemEvent(	String[] arguments, 
							Sequence sequence, WorldObject binding) {
		super(arguments);
		this.binding = binding;
		this.sequence = sequence;
	}

	/*============================================================
	Public Methods
	============================================================*/

	@Override protected void onBegin(WorldArea world) {
		if (arguments() > 0 && binding instanceof GameItemContainer) {
			GameItemContainer container = (GameItemContainer) binding;
			final int itemId = (int) 
					((double) Sequences.parse(sequence, getArgument(0)));
			
			AbstractItem item = ItemGroup.instance().getItem(itemId);
			if (item != null)
				container.setItem((AbstractItem) item.clone());
		} else
			exception("Argument exception.");
		end();
	}

	@Override protected void onEnd() {};

}
